Page({
  data: {
    level: '',
    desc: '',
    history: [],
    avgScore: 0
  },
  onShow() {
    this.loadHistory();
  },
  loadHistory() {
    const history = wx.getStorageSync('quizHistory') || [];
    let avgScore = 0;
    if (history.length > 0) {
      avgScore = history.reduce((sum, h) => sum + h.score, 0) / history.length;
      avgScore = Number(avgScore.toFixed(2));
    }
    let level = '', desc = '';
    if(!history.length) {
      level = '--';
      desc = '您还没有进行过反诈意识评估，请先进行答题。';
    } else if (avgScore >= 80) {
      level = '强';
      desc = '您的反诈意识非常强，能有效识别诈骗行为，继续保持！';
    } else if (avgScore >= 60) {
      level = '中';
      desc = '您的反诈意识一般，请多关注反诈知识，提升防范能力。';
    } else {
      level = '差';
      desc = '您的反诈意识较弱，建议加强学习，谨防上当受骗。';
    }
    this.setData({ history, avgScore, level, desc });
  },
  onClearHistory() {
    wx.showModal({
      title: '确认操作',
      content: '确定要清除所有答题记录吗？',
      success: (res) => {
        if (res.confirm) {
          wx.removeStorageSync('quizHistory');
          this.loadHistory();
        }
      }
    });
  }
}); 